package test2;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.junit.Test;


public class BatchUpdateTest {
    String url = "jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=true";
    String userName = "root";
    String password = "kimheechul";
    String driverName = "com.mysql.jdbc.Driver";
    int num = 1000;
    String strSql = "insert into students(stuno,name,age,birthdate) values(?,?,?,?)";

    
   /*public void slowTest() {
        Connection con = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        SimpleDateFormat hmFromat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");// 格式化日期
        String driverName = "com.mysql.jdbc.Driver";

        try {
            Class.forName(driverName); // 加载MySql的驱动程序
            con = DriverManager.getConnection(url, userName, password);
            con.setAutoCommit(false);
            // 根据参数的插入数据
            pstmt = con.prepareStatement(strSql);
            for (int i = 0; i < num; i++) {
                pstmt.setString(1, "20150111");
                pstmt.setString(2, "Kris");
                pstmt.setInt(3, 27);
                pstmt.setString(4, hmFromat.format(new Date()));
                pstmt.executeUpdate();
            }
            con.commit();

        } catch (SQLException sqlE) {
            sqlE.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (rs != null)
                try {
                    rs.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            if (pstmt != null){
                try {
                    pstmt.close();// 关闭语句*/
                /*} catch (Exception e) {
                    e.printStackTrace();
                }
            }
            if (con != null) {
                try {
                    con.close();// 关闭连接
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            System.out.println("slow test end");
        }
    }*/

  @Test
    public void batchTest() {
    	Long begin = new Date().getTime(); 
        Connection con = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        SimpleDateFormat hmFromat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");// 格式化日期
        String driverName = "com.mysql.jdbc.Driver";

        try {
            Class.forName(driverName); // 加载MySql的驱动程序
            con = DriverManager.getConnection(url, userName, password);

            con.setAutoCommit(false);
            pstmt = con.prepareStatement(strSql);
            for (int i = 0; i < num; i++) {
                pstmt.setString(1, "20150111");
                pstmt.setString(2, "Kris");
                pstmt.setInt(3, 27);
                pstmt.setString(4, hmFromat.format(new Date()));
                pstmt.addBatch();
            }
            pstmt.executeBatch();
            con.commit();

        } catch (SQLException sqlE) {
            sqlE.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (rs != null)
                try {
                    rs.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            if (pstmt != null)
                try {
                    pstmt.close();// 关闭语句*/
                } catch (Exception e) {
                    e.printStackTrace();
                }
            if (con != null) {
                try {
                    con.close();// 关闭连接
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            System.out.println("batch test end");
            Long end = new Date().getTime(); 
            System.out.println("cast : " + (end - begin) / 1000 + " ms"); 
        }
    }

}
